﻿using Pay.Core;
using Pay.DTO;
using Pay.TO;
using Pay.IService;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers {
	public class ManageController : AdminController {
		private IFeatureService _iFeatureService;
		private IRoleService _iRoleService;
		private IManageService _iManageService;

		public ManageController(IFeatureService iFeature, IRoleService roleService, IManageService iManageService) {
			_iFeatureService = iFeature;
			_iRoleService = roleService;
			_iManageService = iManageService;
		}

		#region 功能菜单表
		public JsonResult GetFeature(long id) {
			var result = _iFeatureService.Get(id);
			return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
		}
		public JsonResult GetFeatures(FeatureType id) {
			var features = _iFeatureService.Get(id);
			return Json(ResultToJson.Json(features), JsonRequestBehavior.AllowGet);
		}
		[HttpPut]
		public JsonResult PutFeature(feature_dto entity) {
			var result = _iFeatureService.Update(entity);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
		}
		[HttpPost]
		public JsonResult PostFeature(feature_dto entity) {
			var result = _iFeatureService.Add(entity);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
		}
		[HttpDelete]
		public JsonResult DeleteFeature(long id) {
			var result = _iFeatureService.Delete(id);
			return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
		}
		#endregion

		#region 角色
		public JsonResult GetRole(long id) {
			var result = _iRoleService.Get(id);
			return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
		}
		public JsonResult GetRoles() {
			var Roles = _iRoleService.GetAll(null);
			return Json(ResultToJson.Json(Roles), JsonRequestBehavior.AllowGet);
		}
		[HttpPut]
		[ValidateInput(false)]
		public JsonResult PutRole(long id) {
			var dto = _iRoleService.Get(id);
			TryUpdateModel(dto);
			var result = _iRoleService.Update(dto);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
		}
		[HttpPost]
		[ValidateInput(false)]
		public JsonResult PostRole(role_dto dto) {
			var result = _iRoleService.Add(dto);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
		}
		[HttpDelete]
		public JsonResult DeleteRole(long id) {
			var result = _iRoleService.Delete(id);
			return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
		}
		#endregion

		#region 管理员
		public JsonResult GetManage(long id) {
			var result = _iManageService.Get(id);
			return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
		}
		public JsonResult GetManages() {
			var Manages = _iManageService.Get();
			return Json(ResultToJson.Json(Manages), JsonRequestBehavior.AllowGet);
		}
		[HttpPut]
		[ValidateInput(false)]
		public JsonResult PutManage(long id) {
			var dto = _iManageService.Get(id);
			TryUpdateModel(dto);
			var result = _iManageService.Update(dto);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "没有修改任何内容", result), JsonRequestBehavior.AllowGet);
		}
		[HttpPost]
		[ValidateInput(false)]
		public JsonResult PostManage(manage_dto dto) {
			var result = _iManageService.Add(dto);
			return Json(ResultToJson.Json(result > 0 ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
		}
		[HttpDelete]
		public JsonResult DeleteManage(long id) {
			var result = _iManageService.Delete(id);
			return Json(ResultToJson.Json(result > 0 ? "删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
		}
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Reset(manage_dto dto) {
            var result = _iManageService.Reset(dto);
            return Json(ResultToJson.Json(result > 0 ? "重置成功" : "重置失败", result), JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}